#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define MSG_PATHNAME "."
#define MSG_PROID 2
#define MSG_SZ 64

struct msgbuf{
    long msgtype;
    char msgtext[MSG_SZ];
};

int main(int argc, char const *argv[])
{
    struct msgbuf msg;
    int ret, msg_id;
    key_t key = ftok(MSG_PATHNAME, MSG_PROID);
    // 创建消息队列
    msg_id  = msgget(key, IPC_CREAT | 0666);
    if (msg_id == -1) {
        perror("msgget error \n");
        exit(EXIT_FAILURE);
    }
    msg.msgtype = 101;
    strcpy(msg.msgtext, "hello, world");
   
    // 往消息队列里发送消息
    ret = msgsnd(msg_id, &msg, strlen(msg.msgtext) + 1, 0);
    if (ret == -1) {
        perror("msgsnd error \n");
        exit(EXIT_FAILURE);
    }
    /* code */
    return 0;
}
